package com.fbs.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbs.entity.Permission;
import com.fbs.entity.TbMain;
import com.fbs.entity.User;
import com.fbs.entity.Work;
import com.fbs.mapper.PermissionMapper;
import com.fbs.mapper.TbMainMapper;
import com.fbs.mapper.UserMapper;
import com.fbs.mapper.WorkMapper;
import com.fbs.service.UserService;
import com.fbs.utli.JwtUtil;
import com.fbs.utli.WebUtil;
import com.fbs.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 13745
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-04-19 15:11:11
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
implements UserService {
    @Autowired
    private  UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private TbMainMapper tbMainMapper;
    @Override
    public Result login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(password, user.getPassword());
        if (matches){
            Map<String,Object> map=new HashMap<>();
            map.put("username",user.getName());
            String jwt = JwtUtil.createJWT(map);
            WebUtil.getSession().setAttribute("user",user);
            return new Result(2000,"登录成功");
        }
        return  new Result(5000,"登录失败");

    }
    public Result getPermission(){
        User user = (User) WebUtil.getSession().getAttribute("user");
        List<Permission> permission = permissionMapper.getPermission(user.getRoleid());
        List<Permission> firsts = firsts(permission);
        System.out.println("firsts = " + firsts);
        return new Result(2000,"查询成功",firsts);

    }

    @Override
    public Result modules() {
        List<TbMain> tbMains = tbMainMapper.selectList(null);

        return new Result(2000,"查询成功",tbMains);
    }

    @Override
    public Result delmodules(Integer id) {
        int i = tbMainMapper.deleteById(id);
        if(i!=0){
            return new Result(2000,"删除成功");
        }
        return new Result(5000,"删除失败");

    }

    @Override
    public Result updatemodules(Integer id, Integer status) {
        TbMain tbMain = new TbMain();
        tbMain.setId(id);


        if (status==0){
            tbMain.setStatuss(1);
            int i = tbMainMapper.updateById(tbMain);
        }
        if (status==1){
            tbMain.setStatuss(0);
            int i = tbMainMapper.updateById(tbMain);
        }
        return new Result(2000,"更新状态成功");
    }

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1111");
        strings.add("2222");
        String collect = strings.stream().collect(Collectors.joining(","));
        System.out.println("collect = " + collect);

    }
    private List<Permission> firsts(List<Permission> permissions){
        //1.一级菜单
        List<Permission> firsts=new ArrayList<>();
        for (Permission permission:permissions){
            if(permission.getPid()==0){
                firsts.add(permission);
            }
        }

        //2.查询一级菜单下的二级菜单
        for(Permission first:firsts){
            List<Permission> children=new ArrayList<>();
            for(Permission p:permissions){
                if(first.getId().equals(p.getPid())){
                    children.add(p);
                }
            }
            first.setChildren(children);
        }
        return firsts;
    }
}
